﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2ClassLib;

namespace Ras2Form
{
    public partial class PerformersView : UserControl
    {
        public PerformersView()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                PerformerController controller = new PerformerController();
                foreach (Performer s in controller.GetAllPerformers())
                {
                    m_lvPerformers.Items.Add(CreateListViewItem(s));
                }
            }
        }

        private ListViewItem CreateListViewItem(Performer row)
        {
            ListViewItem item = new ListViewItem(row.Name);

            item.SubItems.Add(row.Description);

            item.ImageIndex = 0;
            item.Tag = row;

            return item;
        }

        public void OnNewPerformer()
        {
            using (PerformerDlg dlg = new PerformerDlg())
            {
                dlg.NewPerformer = true;
                dlg.Performer = new Performer();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Performer performer = dlg.Performer;
                    PerformerController controller = new PerformerController();

                    controller.AddPerformer(performer);
                    m_lvPerformers.Items.Add(CreateListViewItem(performer));
                }
            }
        }
        
        public void OnEditPerformer()
        {
            if (m_lvPerformers.SelectedItems.Count == 1)
            {
                ListViewItem listViewItem = m_lvPerformers.SelectedItems[0];

                Performer performer = (Performer)listViewItem.Tag;
                using (PerformerDlg dlg = new PerformerDlg())
                {
                    dlg.Performer = performer;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        performer = dlg.Performer;
                        PerformerController controller = new PerformerController();

                        controller.UpdatePerformer(performer);
                        int nIndex = listViewItem.Index;

                        m_lvPerformers.Items.Remove(listViewItem);
                        m_lvPerformers.Items.Insert(nIndex, CreateListViewItem(performer));
                    }
                }
            }
        }

        public void OnDeletePerformer()
        {
            if (m_lvPerformers.SelectedItems.Count == 1)
            {
                if (MessageBox.Show("Ertu viss um að þú viljir eyða út þessum flytjanda?", "Eyða flytjanda", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    ListViewItem listViewItem = m_lvPerformers.SelectedItems[0];

                    Performer performer = (Performer)listViewItem.Tag;
                    PerformerController controller = new PerformerController();

                    controller.DeletePerformer(performer);
                    m_lvPerformers.Items.Remove(listViewItem);
                }
            }
        }

        private void OnDoubleClick(object sender, EventArgs e)
        {
            OnEditPerformer();
        }
    }
}
